using TradeFlow.Server.Language;
namespace TradeFlow.Web.Controllers;

/// <summary>
/// 商品信息控制器
/// </summary>
[Authorize(Roles = "None")]
public partial class JxGoodsController : Controller
{
    /// <summary>
    /// 商品服务
    /// </summary>
    private readonly JxGoodsService _goodsService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="goodsService"></param>
    public JxGoodsController(JxGoodsService goodsService)
    {
        _goodsService = goodsService;
    }

    /// <summary>
    /// 商品页面
    /// </summary>
    /// <returns></returns>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询商品分页列表
    /// </summary>
    /// <returns></returns>
    [HttpPost("goods/pages")]
    public async Task<RestfulResult<PageResult<JxGoods>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.upload_data_required);
        JxGoodsPageParam? data = JsonConvert.DeserializeObject<JxGoodsPageParam>(dataStr);
        data ??= new JxGoodsPageParam { PageNo = 1, PageSize = 10 };

        var query = await _goodsService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 获取所有商品列表
    /// </summary>
    /// <returns></returns>
    [HttpPost("goods/list")]
    public async Task<RestfulResult<PageResult<JxGoods>>> GetListAsync()
    {
        var restful = await _goodsService.GetListAsync();
        return restful;
    }

    /// <summary>
    /// 增加商品
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("goods/add")]
    public async Task<RestfulResult> AddAsync([FromBody] JxGoods data)
    {
        var restful = await _goodsService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新商品
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("goods/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] JxGoods data)
    {
        var restful = await _goodsService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除商品
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("goods/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _goodsService.DeleteAsync(data);
        return restful;
    }
}